/**
 * Created by Administrator on 2014/12/26.
 */
function Sokobanfield(container){

    this.boulders=SOKOBANLEVELS[0].boulders;
    this.node=container;
    this.squares=[];
    for(var i=0;i<9;i++) {
        var row = [];
        for (var j = 0; j < 9; j++) {
            var t = SOKOBANLEVELS[0].field[i][j];//# 0 @ *\
            var s = new Square(t,new Point(i,j));
            row.push(s);
            if (s.type == "player") {
                this.playerpos = new Point(i, j);
            }
            this.node.appendChild(s.img);
        }
        this.squares.push(row);
        this.node.appendChild(dom("BR"));
    }
}

Sokobanfield.prototype.won=function(){
    var count=0;
    for(var i=0;i<this.squares.length;i++){
        for(var j=0;j<this.squares.length;j++){
            if(!this.squares[i][j].isshown){
                count++;
            }
        }
    }
    return count<=this.boulders;
}

//处理指定square的反转逻辑
Sokobanfield.prototype.showaround=function(square){
    if(!square.isshown){
        square.show();
        if(square.data=="0"){
            for(var i=0;i<8;i++){
                var newpos=directs[i].add(square.img.pos);
                if(newpos.isvalid()){
                    var newsquare=this.squares[newpos.x][newpos.y];
                    console.log(newpos.x+":"+newpos.y);
                    if(!newsquare.isshown){
                        this.showaround(newsquare);
                    }
                }
            }
        }
    }
}

var directs=[new Point(-1,-1),new Point(-1,0),new Point(-1,1),
    new Point(0,-1), new Point(0,1),
    new Point(1,-1),new Point(1,0),new Point(1,1)
];
